package com.cloudwise.servlet;

import com.cloudwise.service.PersonInfoService;
import com.cloudwise.service.impl.PersonInfoServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈
 * @date ：Created in 2021/1/25 14:42
 * @description：人员信息业务控制器
 * @modified By：
 * @version: 1
 */
//@WebServlet(urlPatterns = "/person")
public class PersonInfoServlet  extends HttpServlet {
    private PersonInfoService personInfoService=new PersonInfoServiceimpl();



    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String values = req.getParameter("key");
        Enumeration<String> attributeNames = req.getParameterNames();
        //封装所有的请求参数名，参数值
        Map map = new HashMap();
       while(attributeNames.hasMoreElements()) {
           /*System.out.println(attributeNames.nextElement());*/
           String  key=attributeNames.nextElement();
           String value = req.getParameter(key);
           map.put(key,value);
       }
        //需要将下拉框的内容填充，区域信息
        List<Map<String, Object>> allArea = personInfoService.findAllArea();
        //判断请求的method
        String  method=map.get("method")+"";
        switch (method){
            case "toAddPage":
                req.setAttribute("allArea",allArea);
                //去添加页面
                req.getRequestDispatcher("addPerson.jsp").forward(req,resp);
                break;
            case "add":
                //保存人员信息
                personInfoService.savePerson(map);
                this.toFindAll(req,resp);
                break;
            case "update":
                //保存人员信息
                personInfoService.updatePerson(map);
                this.toFindAll(req,resp);
                break;
            case "delete":
                //保存人员信息
                personInfoService.deletePerson(map);
                this.toFindAll(req,resp);
                break;
            case "toUpdatePage":
                //去修改页面
                //需要将下拉框的内容填充，区域信息
                Map<String, Object> person = personInfoService.findPersonById(Integer.parseInt(map.get("person_id").toString()));
                req.setAttribute("allArea",allArea);
                req.setAttribute("person",person);
                //转发到修改页面
                req.getRequestDispatcher("updatePerson.jsp").forward(req,resp);
                break;
            default:
                this.toFindAll(req,resp);
                //获取session值
                HttpSession session = req.getSession();
/*                Object xxx = session.getAttribute("xxx");
                System.out.println("session值："+xxx);*/
                String xxx = req.getServletContext().getInitParameter("xxx");
                System.out.println("personinfoServlet："+xxx);
                String ooo = getInitParameter("ooo");
                System.out.println("局部变量："+ooo);
                int maxInactiveInterval = session.getMaxInactiveInterval();
                System.out.println("sesssion的有效期："+maxInactiveInterval+"秒");
                Enumeration<String> attributeNames1 = session.getAttributeNames();
                while(attributeNames1.hasMoreElements()){
                    System.out.println(attributeNames1.nextElement());
                }
                break;
        }




    }
    /**
     * @create by: Teacher陈
     * @description:
     * @create time: 2021/1/25 15:37
     * @param  req, response
     */
    public void toFindAll(HttpServletRequest req,HttpServletResponse response) throws ServletException, IOException {
        //具体业务
        List<Map<String, Object>> allPerson = personInfoService.findAllPerson();
        req.setAttribute("allPerson",allPerson);
        req.getRequestDispatcher("showPerson.jsp").forward(req,response);
    }
}
